package User.FunctionPkg;

import MySQL_im.getInfo;
import Warning.SearchUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Arrays;

public class OwnDatabase implements Win, ActionListener {
    SearchUI searchUI;
    String user;
    ImageIcon icon = new ImageIcon("images/数据库搜索.png");
    getInfo getinfo = new getInfo();
    Connection con = getinfo.getConnection();
    Statement st;
    String[] col = {"时间","体质","平和质","气虚质","阳虚质","阴虚质","痰湿质","湿热质","血瘀质","气郁质","特禀质"};
    String[][] rowData;
    JScrollPane scrollPane;
    JFrame frame=new JFrame();
    JTable table;
    String url = null;
    JButton search = new JButton("搜索",icon);
    public OwnDatabase(String name){
        url = "SELECT * FROM "+name+"_user";
        user = name;
        search.setHorizontalTextPosition(SwingConstants.CENTER);
        search.setVerticalTextPosition(SwingConstants.BOTTOM);
        ResultSet re = null;
        try {
            st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            re = st.executeQuery(url);
            re.last();
            int max = re.getRow();
            rowData = new String[max][col.length];
            for (int i = 0; i < max; i++) {
                re.absolute(i);
                re.next();
                rowData[i][0] = re.getString(1);
                rowData[i][1] = re.getString(2);
                for (int j = 2; j < col.length; j++) {
                    rowData[i][j] = re.getString(j + 1);
                }
            }
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
        table = new JTable(rowData,col);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.getColumnModel().getColumn(0).setPreferredWidth(200);
        table.getColumnModel().getColumn(1).setPreferredWidth(600);
        scrollPane = new JScrollPane(table);
        init();
    }
    @Override
    public void init() {
        frame.setTitle("个人数据库");
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setBounds(558, 138, 735, 750);
        Container c = frame.getContentPane();
        c.add(scrollPane,BorderLayout.CENTER);
        c.add(search,BorderLayout.SOUTH);
        search.setBounds(100,0,60,60);
        search.addActionListener(this);
        frame.setVisible(true);
        searchUI = new SearchUI("搜索栏","请输入搜索内容",this);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==search){
            searchUI.start();
        }
    }
    public void Search(String ans,String date){
        frame.getContentPane().remove(scrollPane);
        url = "SELECT * FROM "+user+"_user WHERE TYPE LIKE ? AND time LIKE ? ";
        search.setHorizontalTextPosition(SwingConstants.CENTER);
        search.setVerticalTextPosition(SwingConstants.BOTTOM);
        ResultSet re = null;
        PreparedStatement pre = null;
        try {
            pre = con.prepareStatement(url,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            pre.setString(1,"%"+ans+"%");
            pre.setString(2,"%"+date+"%");
            re = pre.executeQuery();
            re.last();
            int max = re.getRow();
            System.out.println(max);
            rowData = new String[max][col.length];
            for (int i = 0; i < max; i++) {
                re.absolute(i);
                re.next();
                rowData[i][0] = re.getString(1);
                rowData[i][1] = re.getString(2);
                for (int j = 2; j < col.length; j++) {
                    rowData[i][j] = re.getString(j + 1);
                }
                System.out.println(Arrays.toString(rowData[i]));
            }
            pre.close();
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
        table = new JTable(rowData,col);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.getColumnModel().getColumn(0).setPreferredWidth(200);
        table.getColumnModel().getColumn(1).setPreferredWidth(600);
        scrollPane = new JScrollPane(table);
        Container c = frame.getContentPane();
        c.add(scrollPane,BorderLayout.CENTER);
        c.add(search,BorderLayout.SOUTH);
        search.setBounds(100,0,60,60);
        search.addActionListener(this);
        frame.setVisible(true);
        searchUI.dispose();
    }
}
